package com.mti.flexmusicplayer.controller
{
	import com.mti.flexmusicplayer.filesearch.FileSearch;
	import com.mti.flexmusicplayer.model.MusicModel;
	import com.mti.flexmusicplayer.model.PlayerModel;
	import com.mti.flexmusicplayer.view.PlaylistView;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	
	import spark.collections.Sort;
	import spark.collections.SortField;

	public class PlayerController
	{
		public function PlayerController()
		{
			model = new PlayerModel();
			
			file = File.desktopDirectory;
			
			var types:Array = ["mp3"];
			musicList = new ArrayCollection();
			searcher = new FileSearch(types, musicList);
		}
		
		private var _model:PlayerModel = null;
		[Bindable]
		public function get model():PlayerModel
		{
			return _model;
		}
		public function set model(value:PlayerModel):void
		{
			_model = value;
		}
		
		// #region Play
		
		public function get canPlay():Boolean
		{
			return (model.isPlaying == false) && (musicList.length > 0);
		}
		
		public function play():void
		{
			if (canPlay == false)
				return;
			
			if (model.currentMusic == null)
				model.currentMusic = MusicModel(musicList.getItemAt(0));
			
			model.channel = model.currentMusic.sound.play(model.pausePosition);
			model.channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			model.isPlaying = true;
		}
		
		public function playMusic(music:MusicModel):void
		{
			stop();
			model.currentMusic = music;
			play();
		}
		
		// #endregion Play
		
		// #region Pause
		
		public function get canPause():Boolean
		{
			return model.isPlaying == true;
		}
		
		public function pause():void
		{
			if (canPause == false)
				return;
			
			model.channel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			model.pausePosition = model.channel.position;
			model.channel.stop();
			model.isPlaying = false;
		}
		
		// #endregion Pause
		
		// #region Stop
		
		public function get canStop():Boolean
		{
			return model.currentMusic != null;
		}
		
		public function stop():void
		{
			if (canStop == false)
				return;
			
			if (model.isPlaying)
			{
				model.channel.stop();
				model.channel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			}
			model.pausePosition = 0;
			model.isPlaying = false;
		}
		
		// #endregion Stop
		
		public function onSoundComplete(event:Event):void
		{
			next();
		}
		
		// #region Next
		
		public function get canNext():Boolean
		{
			return musicList.length > 0;
		}
		
		public function next():void
		{
			if (canNext == false)
				return;
			
			var cur:int = currentMusicIndex;
			cur++;
			if (cur == musicList.length)
			{
				cur = 0;
			}
			
			stop();
			model.currentMusic = MusicModel(musicList.getItemAt(cur));
			play();
		}
		
		// #endregion Next
		
		// #region Previous
		
		public function get canPrev():Boolean
		{
			return musicList.length > 0;
		}
		
		public function previous():void
		{
			if (canPrev == false)
				return;
			
			var cur:int = currentMusicIndex;
			--cur;
			if (cur < 0)
			{
				cur = musicList.length - 1;
			}
			
			stop();
			model.currentMusic = MusicModel(musicList.getItemAt(cur));
			play();
		}
		
		// #endregion Previous
		
		// #region File Search
		
		private var searcher:FileSearch = null;
		private var file:File = null;
		
		public function browseDir():void
		{
			file.addEventListener(Event.SELECT,onSelect);
			file.browseForDirectory("Directory Browse");	
		}
		
		public function onSelect(event:Event):void
		{
			file.removeEventListener(Event.SELECT,onSelect);
			searcher.search(file);
		}
		
		public function search(file:File):void
		{
			searcher.search(file);
		}
		
		// #endregion File Search
		
		// #region Playlist
		
		private var _musicList:ArrayCollection = null;
		[Bindable]
		public function get musicList():ArrayCollection
		{
			return _musicList;
		}
		public function set musicList(value:ArrayCollection):void
		{
			_musicList = value;
		}
		
		private function get currentMusicIndex():int
		{
			return musicList.getItemIndex(model.currentMusic);
		}
		
		public function removeMusic(index:int):void
		{
			musicList.removeItemAt(index);
		}
		
		public function sortPlaylist():void
		{
			var authorSortField:SortField = new SortField("author");
			var titleSortField:SortField = new SortField("title");
			var dataSort:Sort = new Sort();
			dataSort.fields = [authorSortField, titleSortField];
			musicList.sort = dataSort;
			musicList.refresh();
		}
		
		// #endregion Playlist
			
	}
}